//手动引入封装的axios  network
import axios from 'axios'
//相当于index.js里面的一个小组件（模块）
const classify = {
    state: {
        navs: [],
        navone: [],
        navtwo:[],
        selectIndex:0
    },
    mutations: {
        getclassify(state, items) {
            state.navs = [...items]
            state.navs.forEach(element => {
                if (element.pid === 0) {
                    state.navone.push(element)
                }
            });
            var id=state.navone[0].id
            state.navs.forEach(element => {
                if (element.pid === id) {
                    state.navtwo.push(element)
                }
            });
        },
        getchange(state, obj) {
            //下标
            state.selectIndex = obj.index;
            //点击一级导航显示二级导航
            state.navtwo=[]
            var id=obj.item.id
            state.navs.forEach(element => {
                if (element.pid === id) {
                    state.navtwo.push(element)
                }
            });
          },
    },
    actions: {
        getclassify(context) {
            axios.post("https://api.it120.cc/small4/shop/goods/category/all")
                .then((res) => {
                    console.log(res.data.data)
                    context.commit("getclassify", res.data.data)
                })
        },
        getchange(context, index) {
            //给getchange传当前下标的值
            context.commit("getchange", index)
          }
    },
    modules: {
    }
}

//抛出module
export default classify;
